package com.example.myapplication.lawyer;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.SearchView;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.viewpager.widget.ViewPager;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.example.myapplication.Config.OKHttpTool;
import com.example.myapplication.R;
import com.example.myapplication.ShuJu.LawyerCarouselmapBean;
import com.example.myapplication.ShuJu.LegalexpertiseBean;
import com.example.myapplication.ShuJu.MyconsultationBean;
import com.example.myapplication.ShuJu.TopchoicelawyeBean;
import com.example.myapplication.apter.LawyerCarouselmapApter;
import com.example.myapplication.apter.LegalexpertiseApter;
import com.example.myapplication.apter.TopchoicelawyeApter;
import com.google.gson.Gson;

public class LawyerActivity extends AppCompatActivity {

    private ImageView but_lawyer;
    private SearchView se_lawyer;
    private ViewPager vp_lawyer;
    private RecyclerView re_lawyer;
    private RecyclerView re_legalexpertise;
    private RecyclerView re_myconsultation;
    private LinearLayout li_center;
    private Gson gson;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_lawyer);
        initview();
        //返回
        but_bloak();
        //轮播图
        carouselmap();
        //法律专长
        Legalexpertise();
        //跳转到咨询列表
        consultationlistadd();
        //本月上榜优选律师
        topchoicelawyer();

    }

    //跳转到咨询列表
    private void consultationlistadd() {
        li_center.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(LawyerActivity.this,ConsultationlistActivity.class);

                LawyerActivity.this.startActivity(intent);
           }
        });
    }

    //查询我的到咨询列表
    /*private void myconsultation() {
        OKHttpTool.doNetRequest("prod-api/api/lawyer-consultation/legal-advice/list",
                OKHttpTool.GET,
                MyconsultationBean.class,
                new OKHttpTool.NetCallback() {
                    @Override
                    public void onSuccess(Object obj) {
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                MyconsultationBean myconsultationBean = (MyconsultationBean) obj;
                                if (myconsultationBean.getCode() == 200) {
                                    //跳转至咨询列表
                                    consultationlistadd(myconsultationBean);
                                }
                            }
                        });
                    }

                    @Override
                    public void onFailure(String msg) {

                    }
                }
        );
    }*/

    private void topchoicelawyer() {
        OKHttpTool.doNetRequest("prod-api/api/lawyer-consultation/lawyer/list-top10",
                OKHttpTool.GET,
                TopchoicelawyeBean.class,
                new OKHttpTool.NetCallback() {
                    @Override
                    public void onSuccess(Object obj) {
                        TopchoicelawyeBean topchoicelawyeBean = (TopchoicelawyeBean) obj;
                        if (topchoicelawyeBean.getCode() == 200) {
                            runOnUiThread(new Runnable() {
                                @Override
                                public void run() {
                                    TopchoicelawyeApter topchoicelawyeApter = new TopchoicelawyeApter(LawyerActivity.this, topchoicelawyeBean.getData());
                                    LinearLayoutManager linearLayoutManager = new LinearLayoutManager(LawyerActivity.this);
                                    re_legalexpertise.setLayoutManager(linearLayoutManager);
                                    re_legalexpertise.setAdapter(topchoicelawyeApter);
                                }
                            });
                        }
                    }

                    @Override
                    public void onFailure(String msg) {

                    }
                });
    }

    private void Legalexpertise() {
        OKHttpTool.doNetRequest("prod-api/api/lawyer-consultation/legal-expertise/list",
                OKHttpTool.GET,
                LegalexpertiseBean.class,
                new OKHttpTool.NetCallback() {
                    @Override
                    public void onSuccess(Object obj) {
                        LegalexpertiseBean legalexpertiseBean = (LegalexpertiseBean) obj;
                        if (legalexpertiseBean.getCode() == 200) {
                            runOnUiThread(new Runnable() {
                                @Override
                                public void run() {
                                    LegalexpertiseApter legalexpertiseApter = new LegalexpertiseApter(LawyerActivity.this, legalexpertiseBean.getRows(), legalexpertiseBean);
                                    re_lawyer.setAdapter(legalexpertiseApter);
                                    GridLayoutManager gridLayoutManager = new GridLayoutManager(LawyerActivity.this, 2);
                                    gridLayoutManager.setOrientation(GridLayoutManager.HORIZONTAL);
                                    re_lawyer.setLayoutManager(gridLayoutManager);
                                }
                            });
                        }
                    }

                    @Override
                    public void onFailure(String msg) {

                    }
                });
    }

    private void carouselmap() {
        OKHttpTool.doNetRequest("prod-api/api/lawyer-consultation/ad-banner/list",
                OKHttpTool.GET,
                LawyerCarouselmapBean.class,
                new OKHttpTool.NetCallback() {
                    @Override
                    public void onSuccess(Object obj) {
                        LawyerCarouselmapBean lawyerCarouselmapBean = (LawyerCarouselmapBean) obj;
                        if (lawyerCarouselmapBean.getCode() == 200) {
                            runOnUiThread(new Runnable() {
                                @Override
                                public void run() {
                                    LawyerCarouselmapApter lawyerCarouselmapApter = new LawyerCarouselmapApter(LawyerActivity.this, lawyerCarouselmapBean.getData());
                                    vp_lawyer.setAdapter(lawyerCarouselmapApter);
                                    mHandler.sendEmptyMessageDelayed(0, 3 * 1000);
                                }
                            });
                        }
                    }

                    @Override
                    public void onFailure(String msg) {

                    }
                });
    }

    private void but_bloak() {
        but_lawyer.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                LawyerActivity.this.finish();
            }
        });
    }

    private void initview() {

        but_lawyer = findViewById(R.id.but_lawyer);
        se_lawyer = findViewById(R.id.se_lawyer);
        vp_lawyer = findViewById(R.id.vp_lawyer);
        re_lawyer = findViewById(R.id.re_lawyer);
        re_legalexpertise = findViewById(R.id.re_legalexpertise);
        re_myconsultation = findViewById(R.id.re_myconsultation);
        li_center = findViewById(R.id.li_center);


        gson = new Gson();

    }


    private Handler mHandler = new Handler() {
        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);

            if (msg.what == 0) {
                int index = vp_lawyer.getCurrentItem();
                index = (index + 1) % 5;
                vp_lawyer.setCurrentItem(index);
                mHandler.sendEmptyMessageDelayed(0, 3 * 1000);
            }
        }
    };
}